/* 
	Copyright (c) 2007, Cliff Rowley, James Deakin
	All rights reserved.
	
	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are
	met:
	
	    * Redistributions of source code must retain the above copyright
	      notice, this list of conditions and the following disclaimer.
	    * Redistributions in binary form must reproduce the above copyright
	      notice, this list of conditions and the following disclaimer in
	      the documentation and/or other materials provided with the
	      distribution.
	    * Neither the name of the <ORGANIZATION> nor the names of its
	      contributors may be used to endorse or promote products derived
	      from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
	A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
	OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package acid.ant;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 * Acid layout generator Ant task.
 * @author cliff <cliffrowley@gmail.com>
 */
public class Layout extends Task {
	
	private String xsl;
	private String xml;
	private String out;

	public void execute() throws BuildException {
		if (xsl == null) {
			throw new BuildException("Argument 'xsl' missing");
		}
		if (xml == null) {
			throw new BuildException("Argument 'xml' missing");
		}
		if (out == null) {
			throw new BuildException("Argument 'out' missing");
		}
		
		try {
			File outf = new File(out);
			
			/* Determine class filename */
			String filename = outf.getName();
			
			/* Determine class name */
			int idx = filename.lastIndexOf('.');
			String classname =
				filename.substring(0, idx);
			
			/* Determine directory name */
			String dirname = out.substring(0, out.length() - filename.length());
			
			/* Determine class package */
			String packagename = dirname.replace('/', '.');
			packagename = packagename.replace('\\', '.');
			
			/* Determine FQ class name */
			String fqclassname = packagename + classname;
			
			/* Blurt it all out */
			System.out.println(xml + " -> " + outf + " (" + fqclassname + ")");
			
			/* Create sources */
			Source xslsrc = new StreamSource(new FileInputStream(xsl));
			Source xmlsrc = new StreamSource(new FileInputStream(xml));
			
			/* Create output directories */
			new File(dirname).mkdirs();
			
			/* Create result */
			Result res = new StreamResult(new FileOutputStream(outf));
			
			/* Create transformer */
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer t = tf.newTransformer(xslsrc);
			
			/* Set text output method */
			t.setOutputProperty("method", "text");
			
			/* Set parameters */
			t.setParameter("filename",    filename);
			t.setParameter("classname",   classname);
			t.setParameter("package",     packagename);
			t.setParameter("fqclassname", fqclassname);
			
			/* Transform document */
			t.transform(xmlsrc, res);
		}
		
		catch (Exception e) {
			//e.printStackTrace();
			throw new BuildException(e);
		}
	}
	
	public void setXsl(String xsl) {
		this.xsl = xsl;
	}
	
	public void setXml(String xml) {
		this.xml = xml;
	}
	
	public void setOut(String out) {
		this.out = out;
	}
	
}
